package com.time.hellotime.components.dynamics;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;


import com.time.hellotime.R;
import com.time.hellotime.db.model.PicModel;
import com.time.hellotime.utils.ScreenUtils;
import com.time.hellotime.utils.ToastUtils;
import com.time.hellotime.webserver.helper.picasso.PicassoImageLoader;
import com.yanzhenjie.nohttp.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2017/7/24.
 * E-mail is tx16bing@163.com
 */

public class PicAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<PicModel> picList;

    public PicAdapter(Context context, List<PicModel> pics) {
        this.mContext = context;
        this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setData(pics);
    }

    private void setData(List<PicModel> pics) {
        if (pics == null) {
            picList = new ArrayList<>();
        } else {
            picList = pics;
        }
    }

    public void updateData(List<PicModel> pics) {
        setData(pics);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return picList.size();
    }

    @Override
    public PicModel getItem(int position) {
        return picList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_dynamics_pic, parent, false);
            holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_publish_pic_one);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PicModel pic = getItem(position);
        if (!TextUtils.isEmpty(pic.getThumbPicAddr())) {

            ViewGroup.LayoutParams lp = holder.ivPic.getLayoutParams();
            int width, height;
            if (getCount() == 1) {
                width = ScreenUtils.getDisplayWidth() - ScreenUtils.dip2px(40);
                height = ScreenUtils.dip2px(167);
                lp.width = width;
                lp.height = height;
                holder.ivPic.setLayoutParams(lp);
                PicassoImageLoader.loadImageViewSize(
                        mContext,
                        pic.getThumbPicAddr(),
                        width,
                        height,
                        R.drawable.iv_default_340_178,
                        R.drawable.iv_default_340_178,
                        toString(),
                        holder.ivPic);
            } else if (getCount() == 2){
                width = (ScreenUtils.getDisplayWidth() - ScreenUtils.dip2px(47)) / 2;
                height = ScreenUtils.dip2px(130);
                lp.width = width;
                lp.height = height;
                holder.ivPic.setLayoutParams(lp);
                PicassoImageLoader.loadImageViewSize(
                        mContext,
                        pic.getThumbPicAddr(),
                        width,
                        height,
                        R.drawable.iv_default_92_68,
                        R.drawable.iv_default_92_68,
                        toString(),
                        holder.ivPic);
            } else{
                width = (ScreenUtils.getDisplayWidth() - ScreenUtils.dip2px(54)) / 3;
                height = width;
                lp.width = width;
                lp.height = height;
                holder.ivPic.setLayoutParams(lp);
                PicassoImageLoader.loadImageViewSize(
                        mContext,
                        pic.getThumbPicAddr(),
                        width,
                        height,
                        R.drawable.iv_default_92_68,
                        R.drawable.iv_default_92_68,
                        toString(),
                        holder.ivPic);
            }

        }
        return convertView;
    }

    private class ViewHolder {
        ImageView ivPic;
    }

}
